package com.lndsnx.ai.controller;

import com.lndsnx.ai.service.FileService;
import com.lndsnx.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author: lndsnx
 * @date: 2025/7/8 21:59
 * @Version: 1.0
 * @description:
 */
@Slf4j
@RestController
@RequestMapping("/ai/file")
public class FileController {

    @Autowired
    private FileService fileService;

    @GetMapping("/chat")
    public String chat(@RequestParam(value = "query") String query, @RequestParam String chatId) {
        log.info("start chat");
        return fileService.chat(query,chatId);
    }

    @GetMapping("/chat-rag-advisor")
    public String chatRagAdvisor(@RequestParam(value = "query") String query, @RequestParam String chatId) {
        log.info("start chat with rag-advisor");
        return fileService.chatRagAdvisor(query,chatId);
    }

    /**
     * 文件上传
     */
    @PostMapping("/upload")
    public Result uploadPdf(@RequestParam String chatId, @RequestParam("file") MultipartFile file) {
        try {
            // 保存文件
            boolean success = fileService.save(chatId, file.getResource());
            if (!success) {
                return Result.fail("保存文件失败！");
            }
            // 3.写入向量库
            // this.writeToVectorStore(file.getResource());
            return Result.ok();
        } catch (Exception e) {
            log.error("上传文件失败.", e);
            return Result.fail("上传文件失败！");
        }
    }
}
